package com.frogs.ucenter.v1.request;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.owasp.esapi.ESAPI;

import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.exception.RequestIllegalException;

/**
 * 用户余额消费请求参数类
 *
 * @author lijian
 * @version 2014年7月31日 - 下午2:48:52
 */
public class UserBalanceConsumeRequest extends Request {

	/**
	 * 用户Id
	 */
	private Integer userId;
	/**
	 * 消费金额
	 */
	private BigDecimal consumeMoney;
	/**
	 * 消费内容
	 */
	private String consumeContent;
	/**
	 * 订单Id
	 */
	private String orderId;
	/**
	 * 验证码
	 */
	private String code;
	
	private String ip;
	
	public UserBalanceConsumeRequest() {
	}
	
	@Override
	public void parse(HttpServletRequest request) {
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		
		this.userId = requestInfo.getIntParameter(ApiUrlParamEnum.userId.getParamName(), 0);
		this.consumeMoney = requestInfo.getBigDecimalParameter(ApiUrlParamEnum.consumeMoney.getParamName());
		this.ip = requestInfo.getParameter(ApiUrlParamEnum.ip.getParamName(), requestInfo.getUserIp());
		this.code = requestInfo.getParameter(ApiUrlParamEnum.code.getParamName(), "");
		this.consumeContent = StringUtil.fixHtml(requestInfo.getParameter(ApiUrlParamEnum.consumeContent.getParamName(), ""));
		this.orderId = requestInfo.getParameter(ApiUrlParamEnum.orderId.getParamName(), "");
		if (userId < 1) {
			throw new RequestIllegalException("userId error " + userId);
		}
		if ("127.0.0.1".equals(ip)) {
			this.ip = requestInfo.getUserIp();
		}
		if (StringUtil.isEmpty(ip)) {
			throw new RequestIllegalException("ip不能为空");
		}
		if (consumeMoney == null || BigDecimal.valueOf(0).compareTo(consumeMoney) == 0 || BigDecimal.valueOf(0).compareTo(consumeMoney) == 1) {
			throw new RequestIllegalException("error consumeMoney: " + consumeMoney);
		}
		if (StringUtil.isEmpty(code)) {
			throw new RequestIllegalException("验证码不能为空");
		}
		if (StringUtil.isEmpty(orderId)) {
			throw new RequestIllegalException("订单号不能为空");
		}
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public BigDecimal getConsumeMoney() {
		return consumeMoney;
	}

	public void setConsumeMoney(BigDecimal consumeMoney) {
		this.consumeMoney = consumeMoney;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getConsumeContent() {
		return consumeContent;
	}

	public void setConsumeContent(String consumeContent) {
		this.consumeContent = consumeContent;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}
